package com.soonfor.evaluate.presenter;
import android.app.Activity;
import android.content.Context;

import com.estronger.shopmanage.ui.othertask.bean.OtherTaskMainBean;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

import com.soonfor.evaluate.bean.EvaluateListBean;
import com.soonfor.evaluate.http.api.Request;
import com.soonfor.evaluate.http.httptools.AsyncUtils;
import com.soonfor.evaluate.activity.fragment.Evaluated_IToCustomersFragment;
import com.soonfor.evaluate.activity.fragment.UnEvaluate_IToCustomersFragment;
import com.soonfor.evaluate.base.IEvalBasePresenter;

/**
 * 作者：DC-DingYG on 2018-10-18 11:29
 * 邮箱：dingyg012655@126.com
 */
public class Evaluate_IToCustomersPresenter implements IEvalBasePresenter, AsyncUtils.AsyncCallback {
    private Activity mActivity;
    private Evaluated_IToCustomersFragment yFragment;
    private UnEvaluate_IToCustomersFragment nFragment;

    public Evaluate_IToCustomersPresenter(Evaluated_IToCustomersFragment yFragment, Activity mActivity) {
        this.yFragment = yFragment;
        this.mActivity = mActivity;
    }

    public Evaluate_IToCustomersPresenter(UnEvaluate_IToCustomersFragment nFragment, Activity mActivity) {
        this.nFragment = nFragment;
        this.mActivity = mActivity;
    }

    /**
     * //获取我对客户的评价列表
     * @param taskatate 评价状态 0待评价 1已评价
     * @param pageNo
     */
    public void getEvaluatedList(Context context, int taskatate, int pageNo, int requestCode) {
        Request.Evaluate.getEvaluateList(context, 1, taskatate, pageNo, requestCode, this);
    }

    @Override
    public void success(int requestCode, String data) {
        final Gson gson = new Gson();
        switch (requestCode) {
            case Request.Evaluate.GET_EVALUATE_ITOCUSTOMER_Y:
                try {
                    JSONArray ja = new JSONArray(data);
                    List<EvaluateListBean> mLists = new ArrayList<>();
                    if(ja!=null && ja.length()>0){
                        for(int i=0; i<ja.length(); i++){
                            JSONObject ja_o = ja.getJSONObject(i);
                            EvaluateListBean mBean = gson.fromJson(ja_o.toString(), EvaluateListBean.class);
                            mLists.add(mBean);
                        }
                    }
                    yFragment.showYList(true,  mLists, null);
                } catch (Exception e) {
                    yFragment.showYList(false,  null, "请求出错");
                }
                yFragment.closeLoadingDialog();
                break;
            case Request.Evaluate.GET_EVALUATE_ITOCUSTOMER_N:
                try {
                    JSONArray ja = new JSONArray(data);
                    List<EvaluateListBean> mLists = new ArrayList<>();
                    if(ja!=null && ja.length()>0){
                        for(int i=0; i<ja.length(); i++){
                            JSONObject ja_o = ja.getJSONObject(i);
                            EvaluateListBean mBean = gson.fromJson(ja_o.toString(), EvaluateListBean.class);
                            mLists.add(mBean);
                        }
                    }
                    nFragment.showNList(true,  mLists, null);
                } catch (Exception e) {
                    nFragment.showNList(false, null, "请求出错");
                }
                nFragment.closeLoadingDialog();
                break;
        }
    }

    @Override
    public void fail(int requestCode, int statusCode, String data, String msg) {
        switch (requestCode) {
            case Request.Evaluate.GET_EVALUATE_ITOCUSTOMER_Y:
                yFragment.showYList(false,null, msg);
                yFragment.closeLoadingDialog();
                break;
            case Request.Evaluate.GET_EVALUATE_ITOCUSTOMER_N:
                nFragment.showNList(false, null, msg);
                nFragment.closeLoadingDialog();
                break;
        }
    }
//    /**
//     * 转为量尺、复尺任务对象
//     *
//     * @param ectBean
//     * @return
//     */
//    public LiangChiBean convertToLcbean(EvaluateListBean ectBean) {
//        if (ectBean != null) {
//            LiangChiBean liangChiBean = new LiangChiBean();
//            liangChiBean.setTaskNo(ectBean.getTaskno());
//            liangChiBean.setTaskType(ectBean.getTasktype());
//            liangChiBean.setOrderNo(ectBean.getOrderno());
//            liangChiBean.setCustomerId(ectBean.getCustomerid());
//            liangChiBean.setCustomerName(ectBean.getCustomername());
//            liangChiBean.setCustomerSex(ectBean.getCustomersex());
//            liangChiBean.setCustomerPhone(ectBean.getCustomerphone());
//            liangChiBean.setAddress(ectBean.getAddress());
//            return liangChiBean;
//        } else {
//            return null;
//        }
//    }
//
//    /**
//     * 转为放样任务对象
//     */
//    public LoftingMainBean convertToFybean(EvaluateListBean ectBean) {
//        if (ectBean != null) {
//            LoftingMainBean loftingMainBean = new LoftingMainBean();
//            loftingMainBean.setTaskNo(ectBean.getTaskno());
//            loftingMainBean.setTaskType(ectBean.getTasktype());
//            loftingMainBean.setOrderNo(ectBean.getOrderno());
//            loftingMainBean.setCustomerId(ectBean.getCustomerid());
//            loftingMainBean.setCustomerName(ectBean.getCustomername());
//            loftingMainBean.setCustomerSex(ectBean.getCustomersex());
//            loftingMainBean.setCustomerPhone(ectBean.getCustomerphone());
//            loftingMainBean.setAddress(ectBean.getAddress());
//            loftingMainBean.setStatus("4");
//            return loftingMainBean;
//        } else {
//            return null;
//        }
//    }
//
//    /**
//     * 转为设计任务对象
//     */
//    public DesignItemBean convertToDesignbean(EvaluateListBean ectBean) {
//        if (ectBean != null) {
//            DesignItemBean design = new DesignItemBean();
//            design.setTaskNo(ectBean.getTaskno());
//            design.setTaskType(ectBean.getTasktype());
//            design.setOrderNo(ectBean.getOrderno());
//            design.setCustomerId(ectBean.getCustomerid());
//            design.setCustomerName(ectBean.getCustomername());
//            design.setCustomerSex(ectBean.getCustomersex());
//            design.setCustomerMobile(ectBean.getCustomerphone());
//            design.setAddress(ectBean.getAddress());
//            design.setStatus("4");
//            return design;
//        } else return null;
//    }
//
    /**
     * 转为其它任务对象
     */
    public OtherTaskMainBean.DataBean convertOtherTaskBean(EvaluateListBean goBean) {
        if (goBean != null) {
            OtherTaskMainBean.DataBean result = new OtherTaskMainBean.DataBean();
            result.setFplantaskid(goBean.getFplantaskid());
            result.setFprocstatedesc("已完成");
            result.setFplantasktype(goBean.getFappraiseoritype());
            return result;
        } else return null;
    }

}